package com.example.zyfypt516wh.model;

import com.example.zyfypt516wh.service.ResService;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.ResIface;
import com.example.zyfypt516wh.iface.ResListener;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 文章类通用
 */
public class ResModel implements ResIface {
    private Retrofit retrofit;
    public ResModel() {
        //使用 Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL).addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getResultList(String mod, int page, String sessionID, ResListener listener) {
        //使用 Retrofit----2
        ResService service=retrofit.create(ResService.class);
        Call<List<ResBean>> call
                =service.getRes(mod,page,sessionID);
        //使用 Retrofit----3
        call.enqueue(new Callback<List<ResBean>>() {
            @Override
            public void onResponse(Call<List<ResBean>> call, Response<List<ResBean>> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }else
                    listener.onFail("on response fail");
            }@Override
            public void onFailure(Call<List<ResBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }



    @Override
    public void getSpecilList(String mod, int page, String sessonID, ResListener listener) {
        //（2）定义请求
        ResService service = retrofit.create(ResService.class);
        Call<List<ResBean>> call = service.getSpeciaList(mod,page,sessonID);
        //（3）发送请求
        call.enqueue(new Callback<List<ResBean>>() {
            @Override
            public void onResponse(Call<List<ResBean>> call, Response<List<ResBean>> response) {
                if (response.isSuccessful() && response != null)
                {
                    listener.onResponse(response.body());
                    System.out.println("--on response size:"+response.body().size());
                }else {
                    listener.onFail("on respone fail");
                    System.out.println("--on respone fail");
                }
            }

            @Override
            public void onFailure(Call<List<ResBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--on failure--"+t.toString());
            }
        });
    }

}
